# -*- coding: utf-8 -*-
"""
用户系统
By AnpyD
"""

"""
Example:
{"pb-None":{"tickets":0,"effects":{"sweat":[2021, 10, 20, 23, 50]},"buffs"{"powerful":{"time":[2021, 10, 20, 23, 50],"multiple":1.6}}}}
"""

# 导入模块
import json
import os
import datetime
import bsInternal
import bs
from game_stats import DATA_PATH


# json 文件路径
account_log_file = DATA_PATH + "/account_information.json"

# 判断文件是否存在
if os.path.exists(account_log_file):
    pass
else:
    FILE = open(account_log_file, "w")
    _basic = {"pb-None":{"tickets":0, "effects":{}, "buffs":{}, "score":0}}
    _basic = json.dumps(_basic)
    FILE.write(_basic)
    FILE.close()

# 提示信息
def account_message(account_id="", text="", type="tip"):
    if type == "tip":
        color = (0, 1.3, 0)
    elif type == "warning":
        color = (1, 0, 0)
    elif type == "inf":
        color = (0, 0, 0.9)
    else:
        color = (0, 0, 0)
    for player in bsInternal._getForegroundHostActivity().players:
        if player.get_account_id() == account_id:
            client_id = player.getInputDevice().getClientID()
            bs.screenMessage(str(text), color=color, clients=[client_id], transient=True)

# 获取 account_data
def get_data():
    """
    return < Python dict >
    """
    FILE = open(account_log_file, "r")
    ACCOUNT_INFORMATION = json.loads(FILE.read())
    FILE.close()
    return ACCOUNT_INFORMATION

# 写入 json
def update_data(data):
    """
    data -> dict
    """
    FILE = open(account_log_file, "w")
    FILE.write(json.dumps(data))
    FILE.close()

# 注册玩家
def register(account_id):
    # Account
    FILE = open(account_log_file, "r")
    ACCOUNT_INFORMATION = json.loads(FILE.read())
    ACCOUNT_INFORMATION[account_id] = {"tickets":0, "effects":{}, "buffs":{}, "score":0}
    FILE.close()
    FILE = open(account_log_file, "w")
    FILE.write(json.dumps(ACCOUNT_INFORMATION))
    FILE.close()

    # Message
    account_message(account_id=account_id, text="你已被服务器注册！感谢游玩本服！", type="tip")


# 添加积分
def add_score(account_id, score):
    # 判断玩家是否注册
    if account_id not in get_data():
        register(account_id)
    # 添加点券
    if score > 0:
        _data = get_data()
        _data[account_id]["score"] += score
        update_data(_data)
        print("点券系统 -> 成功为玩家 " + account_id + " 添加积分!")
    else:
        pass

# 删除积分
def del_score(account_id, score):
    if score > 0:
        _data = get_data()
        _data[account_id]["score"] -= score
        update_data(_data)
        print("点券系统 -> 成功为玩家 " + account_id + " 删除积分!")
    else:
        pass

# 添加点券
def add_tickets(account_id, amount):
    # 判断玩家是否注册
    if account_id not in get_data():
        register(account_id)
    # 添加点券
    if amount > 0:
        _data = get_data()
        _data[account_id]["tickets"] += amount
        update_data(_data)
        print("点券系统 -> 成功为玩家 " + account_id + " 添加点券!")
    else:
        pass

# 删除点券
def del_tickets(account_id, amount):
    # 删除点券
    if amount > 0:
        _data = get_data()
        _data[account_id]["tickets"] -= amount
        update_data(_data)
        print("点券系统 -> 成功为玩家 " + account_id + " 删除点券!")
    else:
        pass

# 获取等级
def get_level(account_id):
    # 判断玩家是否注册
    if account_id not in get_data():
        register(account_id)
    if get_data()[account_id]["score"] <= 300:
        return 1 # 进阶萌新
    elif 300 < get_data()[account_id]["score"] <= 800:
        return 2 # 潜力萌新
    elif 800 < get_data()[account_id]["score"] <= 3200:
        return 3 # 磁暴步兵
    elif 3200 < get_data()[account_id]["score"] <= 5600:
        return 4 # 狂暴杀手
    elif 5600 < get_data()[account_id]["score"]:
        return 5 # 无敌战神

# 比较时间
def check_time(_time):

    # 当前时间
    _now = datetime.datetime.now()
    _year = _now.year
    _month = _now.month
    _day = _now.day
    _hour = _now.hour
    _minute = _now.minute

    # 传入时间
    year = _time[0]
    month = _time[1]
    day = _time[2]
    hour = _time[3]
    minute = _time[4]

    # 比较时间
    if year - _year > 0:
        return True
    else:
        if month - _month >= 0:
            if day - _day >= 0:
                if hour - _hour >= 0:
                    if minute - _minute > 0:
                        return True
                    elif hour - _hour > 0:
                        return True
                    else:
                        return False
                elif day - _day > 0:
                    return True
                else:
                    return False
            elif month - _month > 0:
                return True
            else:
                return False
        elif year - _year > 0:
            return True
        else:
            return False


# 添加/更改 特效
def add_effect(account_id, effect, time):
    # 判断玩家是否注册
    if account_id not in get_data():
        register(account_id)

    # 添加/更新 特效
    _data = get_data()
    _data[account_id]["effects"][effect] = time
    update_data(_data)

# 删除特效
def del_effect(account_id, effect):
    _data = get_data()
    del _data[account_id]["effects"][effect]
    update_data(_data)

# 添加 buff
def add_buff(account_id, buff, time, multiple):
    # 判断玩家是否注册
    if account_id not in get_data():
        register(account_id)

    # 添加/更新 buff
    _data = get_data()
    _data[account_id]["buffs"][buff]["time"] = time
    _data[account_id]["buffs"][buff]["multiple"] = multiple
    update_data(_data)

# 删除 buff
def del_buff(account_id, buff):
    _data = get_data()
    del _data[account_id]["buffs"][buff]
    update_data(_data)
